#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/10 0010 18:29
# @Author  : Arliki
# @File    : process.py

import subprocess
import time
import os
import threading

# child=subprocess.Popen(['ping','-c','5','www.google.com'])
# child.wait()    #子进程等待完成
ch1 = subprocess.Popen('ping www.arliki.com', shell=True)
# ch2 = subprocess.getoutput('python url_test.py')  # 返回值/错误
# ch3 = subprocess.Popen('python error.py', shell=True)  #
print("ch1.pid:", ch1.pid)
t1 = int(time.time())
while ch1.poll() == None:
    time.sleep(1)
    t2 = int(time.time())
    print(t2 - t1)
    if t2 - t1 >= 8:
        print("break")
        break
print("asd")
# print(ch1.stdout.read())
# print(ch1.communicate())
# print(ch2)
t1 = time.time()
time.sleep(2)
print(os.name)
subprocess.Popen('taskkill /f /pid %d /t' % ch1.pid, shell=True)
print(ch1.poll())
print("eqweqweqweqw")
# print('%.2f'%(time.time()-t1))
print(ch1.pid)
print(ch1.poll())
print("______________")


# def tar():
#     print('threading %s is run' % threading.current_thread().name)
#     time.sleep(2)
#     print('threading %s is end' % threading.current_thread().name)
#
#
# def test_lock():
#     print('threading %s is run' % threading.current_thread().name)
#     global a
#     lock.acquire()  # 对资源加锁
#     a += 3
#     lock.release()  # 释放锁
#     print('threading %s is end' % threading.current_thread().name)
#
#
# a = 14
# lock = threading.Lock()  # 调用锁
# print('threading %s is begin' % threading.current_thread().name)
# t1 = threading.Thread(target=tar, name='thread1')
# t2 = threading.Thread(target=test_lock, name='lock_test')
# t1.setDaemon(True)  # 线程开始之前设置守护进程，在不加join时主线程结束将杀死子线程 加上join(s)则等待s秒后结束进程并杀死
# t1.start()  # 子线程1开始
# t2.start()  # 子线程2开始
# print(t1.is_alive())  # 判断子线程是否存活  True、False
# # t.join()
# print('threading %s is to end' % threading.current_thread().name)
# print(t1.is_alive())
# # t.join()
# t1.join(1)
# print(t1.is_alive())
# t2.join()
# print(a)
# print("ttt")
# # ↓↑↑↑↑↑{threading.local} begin
# thread_local = threading.local()
#
#
# def local1():
#     name = thread_local.name
#     print('%s print %s ' % (threading.current_thread().name, name))
#
#
# def local2(name):
#     thread_local.name = name
#     print('%s print a %s' % (threading.current_thread().name, name))
#     local1()
#
#
# t3 = threading.Thread(target=local2, args=('arliki1',), name='local_test1')  # →→→!!{arges后面必须加逗号}!!←←←
# t4 = threading.Thread(target=local2, args=('arliki2',), name='local_test2')  # →→→!!{arges后面必须加逗号}!!←←←
# t3.start()
# t4.start()
# # ↑↑↑↑↑↑{threading.local} end
